﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

public class ReflectUtils
{
    /// <summary>
    /// 获取对应属性的值
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="contents"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static object GetTargetPropertyValue(object obj,string[] contents,int index=0)
    {
        PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
        for (int i = 0; i < propertyInfos.Length; i++)
        {
            if (contents[index] == propertyInfos[i].Name)
            {
                index++;
                if (index< contents.Length)
                {
                   return GetTargetPropertyValue(propertyInfos[i].GetValue(obj, null), contents, index);
                }
                else
                {
                    return propertyInfos[i].GetValue(obj, null);
                }
            }
        }

        return null;
    }

    /// <summary>
    /// 获取对应属性的信息
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="contents"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static PropertyInfo GetTargetPropertyInfo(object obj, string[] contents, int index = 0)
    {
        PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
        for (int i = 0; i < propertyInfos.Length; i++)
        {
            if (contents[index] == propertyInfos[i].Name)
            {
                index++;
                if (index < contents.Length)
                {
                    return GetTargetPropertyInfo(propertyInfos[i].GetValue(obj, null), contents, index);
                }
                else
                {
                    return propertyInfos[i];
                }
            }
        }

        return null;
    }
}
